package com.phome.idao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.phome.idao.IBaseDao;
import com.phome.type.FindType;

/**
 * 
 * dao的实现
 * @author ma
 *
 */
public class BaseDaoImpl<T,PK extends Serializable> implements IBaseDao<T,PK>{

	private int result = 0;
	
	//session的兵工厂
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	private Class<T> classes = null;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 *获取当前的session
	 */
	public Session getSession(){
		System.out.println(sessionFactory);
		return sessionFactory.getCurrentSession();
	}
	
	/*
	 * 获取当前的类
	 */
	@SuppressWarnings("unchecked")
	public BaseDaoImpl(){
		
		this.classes = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}
	
	/*
	 * 增加
	 * (non-Javadoc)
	 * @see com.phome.idao.IBaseDao#add(java.lang.Object)
	 */
	@Override
	public int add(T t) {
		
		result = (Integer)getSession().save(t);
		
		return result;
	}

	/*
	 * 根据id删除
	 * (non-Javadoc)
	 * @see com.phome.idao.IBaseDao#deleteById(java.io.Serializable)
	 */
	@Override
	public void deleteById(PK p) {
		
		//����õĶ���ת��Ϊ����ʵ����Ķ���
		@SuppressWarnings("unchecked")
		T t = (T)getSession().get(classes, p);
		
		//���þ��������ɾ��
		getSession().delete(t);
	}

	/*
	 * �޸�����
	 * (non-Javadoc)
	 * @see com.phome.idao.IBaseDao#update(java.lang.Object)
	 */
	@Override
	public void update(T t) {
		
		getSession().update(t);
	}

	/*
	 * 根据id查询
	 * (non-Javadoc)
	 * @see com.phome.idao.IBaseDao#findById(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public T findById(PK p) {
		
		return (T)getSession().get(classes, p);
	}


	/*
	 * 查询全部
	 * (non-Javadoc)
	 * @see com.phome.idao.IBaseDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> findAll() {
		
		return getSession().createQuery("from"+classes.getName()).list();
	}

	/*
	 * 条件查询
	 * (non-Javadoc)
	 * @see com.phome.idao.IBaseDao#findByCoundition(java.lang.Object)
	 */
	@Override
	public List<T> findByCoundition(FindType type,Object...obj) {
		System.out.println(type);
		for (int i = 0; i < obj.length; i++) {
			System.out.println(obj[i]+"进入dao中的数据为");
		}
		if(type.equals(FindType.USER_PASS)){
			
			return this.findByUsernameAndPassword(obj);
			
		}else if(type.equals(FindType.NAME)){
			
			return this.findByName(obj);
		}
		return null;
	}

	/*
	 * 用户名和密码查询 
	 */
	public List<T> findByUsernameAndPassword(Object...obj){
		
		//获取当前的实体类
		Criteria c = getSession().createCriteria(classes);
		
		//将数据传给当前的实体类对象中
		Criterion c1 = Restrictions.eq("username",obj[0]);
		Criterion c2 = Restrictions.eqOrIsNull("password",obj[1]);
		
		//ʵ调用方法
		c.add(Restrictions.and(c1, c2));
		
		return c.list();
	}
	
	/*
	 * 根号姓名查询
	 */
	@SuppressWarnings("unchecked")
	public List<T> findByName(Object...obj){
		System.out.println("进入");
		//获取当前的实体类并实例化对象
		Criteria c = getSession().createCriteria(classes);
		
		//将数据传给当前的实体类对象
		Criterion ct = Restrictions.eq("name",obj[0]);
		
		//调用方法
		c.add(Restrictions.and(ct));
		List<T> list = c.list();
		return list;
	}
}
